<html>
<head>
    <script src="../../lib/OpenLayers.js"></script>
    <script type="text/javascript">
    function test_Control_SelectFeature_constructor(t) {
        t.plan(2);
        var options = {
//            geometryTypes: "foo"
        };
        var layer = "bar";
        var control = new OpenLayers.Control.SelectFeature(layer, options);
        t.ok(control instanceof OpenLayers.Control.SelectFeature,
             "new OpenLayers.Control.SelectFeature returns an instance");
        t.eq(control.layer, "bar",
             "constructor sets layer correctly");        
//        t.eq(control.featureHandler.geometryTypes, "foo",
//             "constructor sets options correctly on feature handler");
    }
    
    function test_Control_SelectFeature_destroy(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.SelectFeature(layer);
        control.handler.destroy = function() {
            t.ok(true,
                 "control.destroy calls destroy on feature handler");
        }
// should nullify the layer property here
        control.destroy();
        
    }
    
    function test_Control_SelectFeature_select(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.SelectFeature(layer);
        var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0));
        layer.addFeatures(feature);
        layer.drawFeature = function(feature, style) {
            layer.styleMap.createSymbolizer(feature, style);
        }
        control.select(feature);
        t.eq(feature.renderIntent, "select", "render intent is set to select");
        control.unselect(feature);
        t.eq(feature.renderIntent, "default", "render intent is set back to default");
    }
    
    function test_Control_SelectFeature_clickFeature(t) {
        t.plan(4);
        // mock up layer
        var layer = {
            selectedFeatures: [],
            drawFeature: function() {},
            events: {
                triggerEvent: function() {}
            }
        };
        // mock up active control
        var control = new OpenLayers.Control.SelectFeature(layer);
        control.handler = {
            evt: {}
        };
        // mock up features
        var features = new Array(4);
        for(var i=0; i<features.length; ++i) {
            features[i] = {
                id: Math.random(),
                tested: 0,
                style: ""
            };
        }
        
        // test that onSelect gets called properly
        control.onSelect = function(feature) {
            feature.tested += 1;
            t.eq(feature, features[feature.index],
                 "onSelect called with proper feature (" + feature.index + ")");
            t.eq(feature.tested, feature.test,
                 "onSelect called only once for feature (" + feature.index + ")");
        }

        // test that onUnselect gets called properly
        control.onUnselect = function(feature) {
            feature.tested += 1;
            t.eq(feature, features[feature.index],
                 "onUnselect called with proper feature (" + feature.index + ")");
            t.eq(feature.tested, feature.test, 
                 "onUnselect called only once for feature (" + feature.index + ")");
        }
        
        // mock up first click on first feature (runs 2 tests from onSelect)
        var feature = features[0];
        feature.index = 0;
        feature.test = 1;
        control.clickFeature(feature);

        // mock up second click on first feature (runs no tests - already selected)
        control.toggle = false;
        control.clickFeature(feature);

        // mock up second click on first feature (runs 2 tests from onUnselect)
        control.toggle = true;
        feature.test = 2;
        control.clickFeature(feature);

        
    }
    
    function test_Control_SelectFeature_activate(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.SelectFeature(layer);
        map.addControl(control);
        t.ok(!control.handler.active,
             "feature handler is not active prior to activating control");
        control.activate();
        t.ok(control.handler.active,
             "feature handler is active after activating control");
    }

    function test_Control_SelectFeature_deactivate(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.SelectFeature(layer);
        map.addControl(control);
        
        control.activate();
        control.handler.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on feature handler");
        }
        control.deactivate();
    }

    </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>
